<?php

namespace App\Http\Controllers;

use App\Customer;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    use RegistersUsers;

    protected $redirecTo = '/';

    public function __construct()
    {
        $this->middleware('guest:customer');
    }

    public function showRegistrationForm()
    {
        return view('register');
    }

    protected function guard()
    {
        return Auth::guard('customer');
    }

    protected function validator(array $data){
        return \Validator::make($data, [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'string', 'email', 'max:255', 'unique:customers'],
            'gender' => ['required', 'numeric'],
            'phone' => ['required', 'string', 'regex:/(1)[0-9]{10}/', 'unique:customers'],
            'password' => ['required', 'string', 'min:6', 'confirmed'],
        ], [
            'name.required' => '姓名必填',
            'name.max' => '请注意姓名长度',
            'email.required' => '邮箱地址必填',
            'email.max' => '请注意邮箱长度',
            'email.unique' => '邮箱已被注册',
            'gender.required' => '请填写性别',
            'gender.between' => '请填写正确的性别',
            'phone.required' => '手机号码必填',
            'phone.regex' => '请填写正确的手机号码',
            'phone.unique' => '手机号码已被注册',
            'password.required' => '请填写密码',
            'password.min' => '密码太短',
            'password.confirmed' => '两次密码不一致',
        ]);
    }

    protected function create(array $data){
        return Customer::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'gender' => $data['gender'],
            'phone' => $data['phone'],
            'password' => Hash::make($data['password']),
        ]);
    }
}
